package com.huaxin.hxcloud.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.Constants;
import com.huaxin.common.Entity.StationColumnDTO;
import com.huaxin.common.Entity.StationColumnFirst;
import com.huaxin.common.Entity.StationColumnSecond;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.common.tools.Log;
import com.huaxin.hxcloud.R;
import com.huaxindata.common.util.HxLogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author lwjing
 * @date 2018/5/10
 * @Desc 站点树形结构
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxStationColumnActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar      mToolbar;
    @BindView(R.id.rv_station_target)
    RecyclerView rv_station_list;


    ArrayList<MultiItemEntity> data = new ArrayList<>();
    private StationColumnAdapter mExpandableItemAdapter;
    private View                 emptyView;

    @Override
    protected int getContentViewId() {
        return R.layout.act_station_column_list;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    public void initView() {
        setSupportActionBar(mToolbar);
        mExpandableItemAdapter = new StationColumnAdapter(data, this);
        rv_station_list.setAdapter(mExpandableItemAdapter);
        rv_station_list.setLayoutManager(new LinearLayoutManager(this));
        //空布局
        emptyView = LayoutInflater.from(this).inflate(R.layout.item_empty_nodata_center, (ViewGroup) rv_station_list.getParent(), false);

    }

    @Override
    protected void requestData() {
        apiNotStationOrgList();
    }

    private void apiNotStationOrgList() {
        updateLoadStatus(LOADING);
        Call<StationColumnDTO> call = HxTransmitRequestHelper.getRequestApi().getScFindNotStationOrgList();
        autoClose(call);
        call.enqueue(new Callback<StationColumnDTO>() {

            @Override
            public void onResponse(Call<StationColumnDTO> call, Response<StationColumnDTO> response) {
                StationColumnDTO stationTargetDTO = response.body();
                if (response.isSuccessful() && stationTargetDTO != null) {
                    updateLoadStatus(LOAD_SUCCESS);
                    List<StationColumnFirst> stationTarList = stationTargetDTO.getOrgList();
                    if (stationTarList != null && stationTarList.size() > 0) {
                        //数据初始化
                        updateView(stationTarList);
                    } else {
                        mExpandableItemAdapter.setEmptyView(emptyView);
                    }

                } else {
                    updateLoadStatus(LOAD_ERROR);
                }
            }

            @Override
            public void onFailure(Call<StationColumnDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
            }
        });
    }

    /**
     * recycleview 初始化
     *
     * @param stationTarList
     */
    private void updateView(List<StationColumnFirst> stationTarList) {
        data.clear();
        for (int i = 0; i < stationTarList.size(); i++) {
            //第一层
            StationColumnFirst stationlist = stationTarList.get(i);
            //第二层
            List<StationColumnSecond> stationColumn = stationlist.getChildren();
            if (stationColumn != null && stationColumn.size() > 0) {
                for (int j = 0; j < stationColumn.size(); j++) {
                    data.add(ColumnData(stationColumn.get(j), 0));
                }
            }
        }
        if (data.size() == 0) {
            //设置空布局
            mExpandableItemAdapter.setEmptyView(emptyView);
        } else {
            mExpandableItemAdapter.setNewData(data);
        }

    }

    private StationColumnSecond ColumnData(StationColumnSecond stationColumn, int level) {
        //第二层
        stationColumn.setLevel(level);
        HxLogUtils.i("--stationColumn-->" + stationColumn.toString());
        List<StationColumnSecond> stationColumnNextList = stationColumn.getChildren();
        if (stationColumnNextList != null && stationColumnNextList.size() > 0) {
            if (level == 0) {
                stationColumn.setItemType(StationColumnAdapter.ColumnStationFirst);
            } else {
                stationColumn.setItemType(StationColumnAdapter.ColumnStation);
            }
            for (int j = 0; j < stationColumnNextList.size(); j++) {
                StationColumnSecond stationColumnNext = stationColumnNextList.get(j);
                //  stationColumnNext.setLevel(level);
                stationColumnNext = ColumnData(stationColumnNext, level + 1);
                stationColumn.addSubItem(stationColumnNext);
            }
        } else {
            if (level == 0) {
                stationColumn.setItemType(StationColumnAdapter.ColumnStationFirst);
            } else if (level == 1) {
                stationColumn.setItemType(StationColumnAdapter.ColumnStation);
            } else {
                stationColumn.setItemType(StationColumnAdapter.ColumnStationEnd);
            }
        }
        Log.i("lwjing", "ColumnData level= " + level + " " + stationColumn.getItemType() + "---id--" + stationColumn.getId());
        return stationColumn;
    }

    public class StationColumnAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
        //数据第一层
        public static final int ColumnStationFirst = 0;
        //数据中间层
        public static final int ColumnStation      = 10;
        //数据最里层
        public static final int ColumnStationEnd   = 20;
        public              int padding            = 40;

        private final Context mContext;
        //层级数
        private       int     mStationColumnLevel;

        public StationColumnAdapter(List<MultiItemEntity> data, Context context) {
            super(data);
            this.mContext = context;
            addItemType(ColumnStationFirst, R.layout.item_station_first);
            addItemType(ColumnStation, R.layout.item_station_second);
            addItemType(ColumnStationEnd, R.layout.item_station_end);

        }

        @Override
        protected void convert(final BaseViewHolder holder, MultiItemEntity multiItemEntity) {
            switch (holder.getItemViewType()) {
                case ColumnStationFirst:
                    //第一层级
                    final StationColumnSecond stationColumnFirst = (StationColumnSecond) multiItemEntity;
                    TextView tvfirst = holder.getView(R.id.tv_first);
                    //
                    tvfirst.setPadding(padding, 0, 0, 0);
                    holder.setText(R.id.tv_first, stationColumnFirst.getText() + " " + stationColumnFirst.getLevel());
                    //箭头
                    ImageView ivExpandArrow = holder.getView(R.id.img_expand_arrow);
                    if (stationColumnFirst.getChildren() == null) {
                        ivExpandArrow.setVisibility(View.GONE);
                    } else {
                        ivExpandArrow.setVisibility(View.VISIBLE);
                    }
                    tvfirst.setText(stationColumnFirst.getText());
                    if (stationColumnFirst.isExpanded()) {
                        holder.setImageResource(R.id.img_expand_arrow, R.drawable.arrow_r);
                    } else {
                        holder.setImageResource(R.id.img_expand_arrow, R.drawable.arrow_b);
                    }
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int pos = holder.getAdapterPosition();
                            if (stationColumnFirst.getSubItems() == null || stationColumnFirst.getSubItems().size() == 0) {
                                //  ToastUtil.showToast(HxStationColumnActivity.this,"no column next");
                                return;
                            }
                            if (stationColumnFirst.isExpanded()) {
                                collapse(pos);
                            } else {
                                expand(pos);
                            }
                            notifyDataSetChanged();
                        }
                    });

                    break;
                case ColumnStation:
                    //TODO 第二 三 四 五......层级
                    final StationColumnSecond stationColumn = (StationColumnSecond) multiItemEntity;
                    TextView tvSecond = holder.getView(R.id.tv_second);
                    mStationColumnLevel = stationColumn.getLevel();
                    tvSecond.setPadding(padding * stationColumn.getLevel(), 0, 0, 0);
                    //箭头
                    ImageView ivExpandArrowSecond = holder.getView(R.id.iv_expand_arrow);

                    tvSecond.setText(stationColumn.getText());
                    if (stationColumn.isExpanded()) {
                        holder.setImageResource(R.id.iv_expand_arrow, R.drawable.arrow_r);
                    } else {
                        holder.setImageResource(R.id.iv_expand_arrow, R.drawable.arrow_b);
                    }
                    if (stationColumn.getChildren() == null) {
                        ivExpandArrowSecond.setVisibility(View.GONE);
                    }
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int pos = holder.getAdapterPosition();
                            if (stationColumn.getChildren() == null) {
                                intentStationList(stationColumn.getId().substring(4), stationColumn.getText());
                            }
                            notifyDataSetChanged();

                            if (stationColumn.isExpanded()) {
                                collapse(pos);
                            } else {
                                expand(pos);
                            }

                        }
                    });
                    break;
                case ColumnStationEnd:
                    //TODO 最后一层
                    final StationColumnSecond stationColumn3 = (StationColumnSecond) multiItemEntity;
                    TextView tvSecondEnd = holder.getView(R.id.tv_second);
                    holder.getView(R.id.tv_station_falg).setPadding(padding * (mStationColumnLevel + 1), 0, 0, 0);
                    tvSecondEnd.setText(stationColumn3.getText());
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            intentStationList(stationColumn3.getId().substring(4), stationColumn3.getText());
                        }
                    });
                    break;
            }

        }

        private void intentStationList(String id, String title) {
            Intent intent = new Intent(mContext, HxStationListActivity.class);
            HxLogUtils.i("stationCode3---->" + id);
            intent.putExtra(Constants.IK_STATION_CODE, id);
            intent.putExtra(Constants.IK_STATION_NAME, title);
            startActivity(intent);
        }
    }


}
